menuentry 'Load Operating System' {
	insmod ahci
	insmod part_msdos
	insmod part_gpt
	# Iterate through all possible disks and partitions
	for i in 0 1; do
		# Look for fully encrypted disks, prompt for passphrase if successful
		cryptomount "(ahci${i})"
		# Check for filesystems replacing MBR/GPT at all, e.g. BTRFS or ZFS
		x="(crypto${i})"
		set root=${x}
		for p in "grub/libreboot_" "boot/grub/libreboot_" "grub/" "boot/grub/"; do
			if [ -f "${x}/${p}grub.cfg" ] ; then
				configfile /${p}grub.cfg
			fi
		done
		for j in 0 1 2 3 4 5 6 7 8 9; do
		# Check for normal MBR/GPT disks
		# GPT allows more than 4 partitions, /boot on /dev/sda7 is highly unlikely but still possible
			# /boot can still be encrypted
			for k in "crypto" "ahci"; do
				x="(${k}${i},${j})"
				set root=${x}
				for p in "grub/libreboot_" "boot/grub/libreboot_" "grub/" "boot/grub/"; do
					if [ -f "${x}/${p}grub.cfg" ] ; then
						configfile /${p}grub.cfg
					fi
				done
			done
		done
	done

# Last resort, otherwise go to GRUB shell
	set root='ahci0,1'
	for p in "/" "/boot/"; do
		if [ -f "${p}vmlinuz" ] ; then
			linux ${p}vmlinuz root=/dev/sda1 rw
			if [ -f "${p}initrd.img" ] ; then
				initrd ${p}initrd.img
			fi
		fi
	done
}
menuentry 'Parse ISOLINUX menu (SATA)' {
	insmod ahci
	insmod part_msdos
	insmod part_gpt
	for i in 0 1; do
		# Check for filesystems replacing MBR/GPT at all, e.g. BTRFS or ZFS
		set root="(ahci${i})"
		for p in "/isolinux" "/syslinux"; do
			if [ -f "${p}${p}.cfg" ] ; then
				syslinux_configfile -i ${p}${p}.cfg
			elif [ -f "/boot${p}${p}.cfg" ] ; then
				syslinux_configfile -i /boot${p}${p}.cfg
			fi
		done
		# Check for normal MBR/GPT disks
		# GPT allows more than 4 partitions, /boot on /dev/sda7 is highly unlikely but still possible
		for j in 0 1 2 3 4 5 6 7 8 9; do
			set root="(ahci${i},${j})"
			for p in "/isolinux" "/syslinux"; do
				if [ -f "${p}${p}.cfg" ] ; then
					syslinux_configfile -i ${p}${p}.cfg
				elif [ -f "/boot${p}${p}.cfg" ] ; then
					syslinux_configfile -i /boot${p}${p}.cfg
				fi
			done
		done
	done
}
menuentry 'Parse ISOLINUX menu (USB)' {
	insmod usbms
	insmod part_msdos
	insmod part_gpt
	for i in 0 1; do
		# Check for filesystems replacing MBR/GPT at all, e.g. BTRFS or ZFS
		set root="(usb${i})"
		for p in "/isolinux" "/syslinux"; do
			if [ -f "${p}${p}.cfg" ] ; then
				syslinux_configfile -i ${p}${p}.cfg
					elif [ -f "/boot${p}${p}.cfg" ] ; then
				syslinux_configfile -i /boot${p}${p}.cfg
			fi
		done
		# Check for normal MBR/GPT disks
		# GPT allows more than 4 partitions, /boot on /dev/sda7 is highly unlikely but still possible
		for j in 0 1 2 3 4 5 6 7 8 9; do
			set root="(usb${i},${j})"
			for p in "/isolinux" "/syslinux"; do
				if [ -f "${p}${p}.cfg" ] ; then
					syslinux_configfile -i ${p}${p}.cfg
				elif [ -f "/boot${p}${p}.cfg" ] ; then
					syslinux_configfile -i /boot${p}${p}.cfg
				fi
			done
		done
	done
}
menuentry 'Parse ISOLINUX menu (CD/DVD)' {
	insmod ahci
	insmod ata
	insmod iso9660
	for x in (ata0) (ahci1); do
		set root=${x}
		for p in "/isolinux" "/syslinux"; do
			if [ -f "${p}${p}.cfg" ] ; then
				syslinux_configfile -i ${p}${p}.cfg
			elif [ -f "/boot${p}${p}.cfg" ] ; then
				syslinux_configfile -i /boot${p}${p}.cfg
			fi
		done
	done
}
menuentry 'Switch to grubtest.cfg' {
	set root='cbfsdisk'
	configfile (cbfsdisk)/grubtest.cfg
}
menuentry 'Search for GRUB configuration (grub.cfg) outside of CBFS' {
	insmod ahci
	insmod usbms
	insmod part_msdos
	insmod part_gpt
	for i in ahci0 usb0 usb1; do
		for j in 1 2 3 4 5 6 7 8 9; do
			x="(${i},${j})"
			for p in "grub" "boot/grub" "grub2" "boot/grub2"; do
				if [ -f "${x}/${p}/grub.cfg" ] ; then
					submenu "Load Config from ${x}" ${x} {
						root=$2
						source /${p}/grub.cfg
						unset superusers
					}
				fi
			done
		done
	done
}
menuentry 'Poweroff' {
halt
}
menuentry 'Reboot' {
reboot
}
